package org.firebug.spring.boot.metadata.api.controller;

import org.firebug.common.bean.api.Meta;
import org.firebug.common.bean.api.Response;
import org.firebug.spring.boot.metadata.api.vo.DictionarySaveVO;
import org.firebug.spring.boot.metadata.api.vo.DictionaryVO;
import org.firebug.spring.boot.metadata.service.DictionaryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典服务
 */
@RestController
@RequestMapping(value = {"/dictionary"})
public class DictionaryController {
    @Resource
    private DictionaryService dictionaryService;

    @RequestMapping(value = "/save", method = {RequestMethod.POST}, params = {"version=1"})
    public Response save(@RequestBody @Validated DictionarySaveVO vo) {
        dictionaryService.save(vo);
        return Response._200;
    }

    @RequestMapping(value = "/query/{code}", method = {RequestMethod.GET}, params = {"version=1"})
    public Response<DictionaryVO> query(@PathVariable String code) {
        return Response.newInstance(Meta._200, dictionaryService.queryByCode(code));
    }

    @RequestMapping(value = "/list/{code}", method = {RequestMethod.GET}, params = {"version=1"})
    public Response<List<DictionaryVO>> list(@PathVariable("code") String code) {
        return Response.newInstance(Meta._200, dictionaryService.listByPcode(code));
    }

    @RequestMapping(value = "/list/{code}/{sector}", method = {RequestMethod.GET}, params = {"version=1"})
    public Response<List<DictionaryVO>> list(@PathVariable("code") String code, @PathVariable("sector") int sector) {
        return Response.newInstance(Meta._200, dictionaryService.listByCodeSector(code, sector));
    }
}